package tool

import (
	"reflect"
)

// InitTag2FieldIdx 运用反射获取结构体字段
func InitTag2FieldIdx(v interface{}, tagKey string) map[string]int {
	u := reflect.TypeOf(v)
	numField := u.NumField()
	tag2fieldIndex := map[string]int{}
	for i := 0; i < numField; i++ {
		f := u.Field(i)
		tagValue, ok := f.Tag.Lookup(tagKey)
		if ok {
			tag2fieldIndex[tagValue] = i
		} else {
			continue
		}
	}
	return tag2fieldIndex
}
